<?php
require_once("BaseSnack.php");
require_once("MysqlConn.php");

class Snack_W extends BaseSnack {
   public function setID($id) {
      $this->id = $id;
   }

   public function setName($name) {
      $this->name = $name;
   }

   public function setPrice($price) {
      $this->price = $price;
   }

   public function setDescription($desc) {
      $this->desc = $desc;
   }

   public function setType($type) {
      $this->type = $type;
   }

   public function setPoints($points) {
      $this->points = $points;
   }

   public function removeDB() {
      if($this->id != null && is_numeric($this->id)) {
         $query = "delete from Snacks where SNA_ID=$this->id;";
         $conn = new MysqlConn();
         $conn->query($query);
         return $conn->getErrNo() > 0 ? $conn->getErrNo() : 0;
      }

      return -1;
   }

   public function updateDB() {
      $sets = "";

      if($this->id != null && is_numeric($this->id)) {
         if($this->name != null) {
            $sets .= ", SNA_NAME='$this->name' ";
         }

         if($this->type != null && is_numeric($this->type)) {
            $sets .= ", SNA_TYPE=$this->type ";
         }

         if($this->price != null && is_numeric($this->price)) {
            $sets .= ", SNA_PRICE=$this->price ";
         }

         if($this->desc != null) {
            $sets .= ", SNA_DESC='$this->desc' ";
         }

         $sets = substr($sets, 2);
         $query = "update Snacks set $sets where SNA_ID=$this->id;";
         $conn = new MysqlConn();
         $conn->query($query);

         return $conn->getErrNo() > 0 ? $conn->getErrNo() : 0;
      }

      return -1;
   }

   public function insertDB() {
      $query = "Insert into Snacks (SNA_NAME, SNA_PRICE, SNA_DESC, SNA_TYPE) " .
         "values ('$this->name', $this->price, '$this->desc', $this->type)";
      $conn = new MysqlConn();
      $conn->query($query);

      return $conn->getErrNo() > 0 ? $conn->getErrNo() : 0;
   }
}
?>